<?php
// Comment functions

/**
 * framework_discussion_title - 
 *
 * @since 0.3
 * @needsdoc
 * @filter framework_many_comments, framework_no_comments, framework_one_comment, framework_comments_number
 */
function framework_discussion_title( $type = NULL, $echo = true ) {
	if ( !$type ) return;

	$comment_count = framework_count( 'comment', false );
	$ping_count = framework_count( 'pings', false );

	switch( $type ) {
		case 'comment' :
			$count = $comment_count;
			$many  = apply_filters( 'framework_many_comments', (string) '% Comments' ); // Available filter: framework_many_comments
			$none  = apply_filters( 'framework_no_comments', (string) 'No Comments yet' ); // Available filter: framework_no_comments
			$one   = apply_filters( 'framework_one_comment', (string) '1 Comment' ); // Available filter: framework_one_comment
			break;
		case 'pings' :
			$count = $ping_count;
			$many  = apply_filters( 'framework_many_pings', (string) '% Trackbacks' ); // Available filter: framework_many_pings
			$none  = apply_filters( 'framework_no_pings', (string) '' ); // Available filter: framework_no_pings
			$one   = apply_filters( 'framework_one_ping', (string) '1 Trackback' ); // Available filter: framework_one_comment
			break;
	}
	
	if ( $count > 1 ) {
		$number = str_replace( '%', number_format_i18n( $count ), $many );
	} elseif ( $count == 0 ) {
		$number = $none;
	} else { // it must be one
		$number = $one;
	}
	
	// Now let's format this badboy.
	$tag = apply_filters( 'framework_discussion_title_tag', (string) 'h3' ); // Available filter: framework_discussion_title_tag
	
	if ( $number ) {
		$discussion_title  = '<'. $tag .' class="'. $type .'-title">';
		$discussion_title .= $number . ' to ';
		$discussion_title .= '<span class="'. $type .'-title-meta">&#8220;</span>';
		$discussion_title .= get_the_title();
		$discussion_title .= '<span class="'. $type .'-title-meta">&#8221;</span></'. $tag .'>';
	}
	$framework_discussion_title = apply_filters( 'framework_discussion_title', (string) $discussion_title ); // Available filter: framework_discussion_title
	return ( $echo ) ? print( $framework_discussion_title ) : $framework_discussion_title;
}

/**
 * framework_count() - 
 *
 * @since 0.3
 * @needsdoc
 */
function framework_count( $type = NULL, $echo = true ) {
	if ( !$type ) return;
	global $wp_query;

	$comment_count = count( $wp_query->comments_by_type['comment'] );
	$ping_count = count( $wp_query->comments_by_type['trackback'] );
	
	switch ( $type ):
		case 'comment':
			return ( $echo ) ? print( $comment_count ) : $comment_count;
			break;
		case 'pings':
			return ( $echo ) ? print( $ping_count ) : $ping_count;
			break;
	endswitch;
}

/**
 * framework_comment_author() - short description
 *
 * @since - 0.3
 * @todo needs filter
 */
function framework_comment_author( $avatar = true, $gravatar_size = 50 ) {
	// Get comentor's details
	$commenter = get_comment_author_link();
	$author = get_comment_author();
	$url = get_comment_author_url();

	if ( ! empty( $url ) || 'http://' ==! $url ) {
		$uri = ' href="'. $url .'" ';
	} else {
		$uri = NULL;
	}
	if ( $avatar ) {
		// Get author's gavatar
		$gravatar_email = get_comment_author_email();
		$gravatar_size = apply_filters( 'framework_gravatar_size', $gravatar_size ); // Available filter: framework_gravatar_size
		$gravatar = get_avatar( $gravatar_email, $gravatar_size );
		
		// get the $src data from $gavatar
		if ( preg_match( '/src=\'(.*?)\'/i', $gravatar, $matches ) )
			$src = $matches[1];
		
		// Rebuild Gravatar link because get_avatar() produces invalid code :/ (P.S. adds "gravatar" class)
		echo "<a class=\"avatar-link\"{$uri}><img class=\"avatar gravatar gravatar-{$gravatar_size}\" alt=\"{$author}'s Gravatar\" src=\"{$src}\" width=\"{$gravatar_size}\" height=\"{$gravatar_size}\" /></a>";
	}
	// Commentor vcard
	echo "<cite class=\"commenter fn n\">{$commenter}</cite>";
}

/**
 * framework_comments_rss - 
 *
 * @since 0.3
 * @needsdoc
 */
function framework_comments_rss() {
	global $id;
	$uri = get_post_comments_feed_link( $id );
	$text = "<p class=\"comment-feed-link\">You can follow all the replies to this entry through the <a href=\"{$uri}\">comments</a> feed.</p>";
	echo $text;
}

/**
 * framework_comment_navigation - paged comments
 *
 * @since 0.3
 * @needsdoc
 * @todo add html comments?
 */
function framework_comment_navigation(){
	$num = get_comments_number() + 1;
	
	$tag = apply_filters( 'framework_comment_navigation_tag', (string) 'div' ); // Available filter: framework_comment_navigation_tag
	$open = "<!--BEGIN .navigation-links-->\n";
	$open .= "<". $tag ." class=\"navigation-links comment-navigation\">\n";
	$close = "<!--END .navigation-links-->\n";
	$close .= "</". $tag .">\n";
	
	if ( $num > get_option( 'comments_per_page' ) ) {		
		$paged_links = paginate_comments_links( array(
			'type' => 'array',
			'echo' => false,
			'prev_text' => '&laquo; Previous Page',
			'next_text' => 'Next Page &raquo;' ) );
		
		if ( $paged_links ) $comment_navigation = $open . join( ' ', $paged_links ) . $close;
	}
	else {
		$comment_navigation = NULL;
	}
	echo apply_filters( 'framework_comment_navigation', (string) $comment_navigation ); // Available filter: framework_comment_navigation
}

/**
 * framework_comments_callback - recreate the comment list
 *
 * @since 0.3
 * @needsdoc
 */
function framework_comments_callback( $comment, $args, $depth ) {	
	$GLOBALS['comment'] = $comment;
	$GLOBALS['comment_depth'] = $depth;
	$tag = apply_filters( 'framework_comments_list_tag', (string) 'li' ); // Available filter: framework_comments_list_tag
	?>
    
    <!--BEING .comment-->
	<<?php echo $tag; ?> class="<?php framework_comment_class(); ?>" id="comment-<?php echo comment_ID(); ?>">
		
        <!--BEGIN .comment_author-->
        <div class="comment_author vcard">
			<?php framework_comment_author(); ?>
        	
            <!--BEGIN .comment-meta-->
            <span class="comment-meta">
                <abbr class="comment-date" title="<?php comment_time( 'Y-m-d\TH:i:sO' ); ?>"><a rel="bookmark" href="<?php echo htmlspecialchars( get_comment_link() ); ?>"><?php comment_date(); ?></a></abbr>
                <?php edit_comment_link( 'edit', '<span class="edit-comment">', '</span>' ); ?>
            <!--END .comment-meta-->
            </span>
        <!--END .comment_author-->
        </div>
        <?php if ( $comment->comment_approved == '0' ) echo '<p class="comment-unapproved moderation alert">Your comment is awaiting moderation.</p>\n'; ?>
        
        <!--BEGIN .comment-content-->
        <div class="comment-content">
        	<?php comment_text(); ?>
        <!--END .comment-content-->
        </div>
		
		<?php // Comment Reply + Edit
        if ( ( get_option( 'thread_comments' ) ) && ( $args['type'] == 'all' || get_comment_type() == 'comment' ) ) {
            $max_depth = get_option( 'thread_comments_depth' );
            echo comment_reply_link( array(
                'reply_text' => 'Reply', 
                'login_text' => 'Log in to reply.',
                'depth' => $depth,
                'max_depth' => $max_depth, 
                'before' => '<span class="comment-reply">', 
                'after' => '</span>'
            )); }
}

/**
 * framework_comments_endcallback() - close the comment list
 *
 * @since - 0.3
 * @needsdoc
 * @todo needs filter
 */
function framework_comments_endcallback(){
	$tag = apply_filters( 'framework_comments_list_tag', (string) 'li' ); // Available filter: framework_comments_list_tag
	echo "<!--END .comment-->\n";
	echo "</". $tag .">\n";
	do_action( 'framework_hook_inside_comments_loop' ); // Available hook: framework_hook_inside_comments_loop
}

/**
 * framework_pings_callback - recreate the comment list
 *
 * @since 0.3
 * @needsdoc
 */
function framework_pings_callback( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	$tag = apply_filters( 'framework_pings_callback_tag', (string) 'li' ); // Available filter: framework_pings_callback_tag
	$byline = apply_filters( 'framework_pings_callback_byline', (string) 'By ' ); // Available filter: framework_pings_callback_byline
	$time = apply_filters( 'framework_pings_callback_time', (string) ' on ' ); // Available filter: framework_pings_callback_time
	$when = apply_filters( 'framework_pings_callback_when', (string) ' at ' ); // Available filter: framework_pings_callback_time
	?>
    <?php if ( $comment->comment_approved == '0' ) echo '<p class="ping-unapproved moderation alert">Your trackback is awaiting moderation.</p>\n'; ?>
    <!--BEING .pings-->
	<<?php echo $tag; ?> class="<?php echo framework_comment_class(); ?>" id="ping-<?php echo $comment->comment_ID; ?>">
		<?php echo $byline; framework_comment_author( false ); echo $time; ?><a class="trackback-time" href="<?php comment_link(); ?>"><?php comment_date(); echo $when; comment_time(); ?></a>
	<?php
}

/**
 * framework_pings_endcallback() - close the comment list
 *
 * @since - 0.3
 * @needsdoc
 * @todo needs filter
 */
function framework_pings_endcallback(){
	$tag = apply_filters( 'framework_pings_callback_tag', (string) 'li' ); // Available filter: framework_pings_callback_tag
	echo "<!--END .pings-list-->\n";
	echo "</". $tag .">\n";
	do_action( 'framework_hook_inside_pings_list' ); // Available hook: framework_hook_inside_pings_list
}
?>